package com.itaming.lycheeframework.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.itaming.lycheeframework.support.utils.StringPool;

import java.io.IOException;

/**
 * <code>null</code>转空值序列化
 *
 * @author A.Ming
 */
public interface NullToEmptySerializers {

    /**
     * 字符串序列化器，<code>null</code>转为<code>""</code>
     */
    JsonSerializer<Object> STRING_JSON_SERIALIZER = new JsonSerializer<>() {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(StringPool.EMPTY);
        }
    };

    /**
     * 数字序列化器，<code>null</code>转为<code>-1</code>
     */
    JsonSerializer<Object> NUMBER_JSON_SERIALIZER = new JsonSerializer<>() {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeNumber(-1);
        }
    };

    /**
     * 布尔序列化器，<code>null</code>转为{@link Boolean#FALSE}
     */
    JsonSerializer<Object> BOOLEAN_JSON_SERIALIZER = new JsonSerializer<>() {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeObject(Boolean.FALSE);
        }
    };

    /**
     * 数组序列化器，<code>null</code>转为<code>[]</code>
     */
    JsonSerializer<Object> ARRAY_JSON_SERIALIZER = new JsonSerializer<>() {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeStartArray();
            jsonGenerator.writeEndArray();
        }
    };

    /**
     * 对象序列化器，<code>null</code>转为<code>{}</code>
     */
    JsonSerializer<Object> OBJECT_JSON_SERIALIZER = new JsonSerializer<>() {
        @Override
        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeEndObject();
        }
    };

}
